let layoutComponent = null
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageStatus: {
            layoutStatus: null,
            loading: false,
            loadingMore: false
        },
        type: {    //优惠券使用状态
            active: 0,
            list: [
                {
                    name: '未使用',
                    num: 0,
                    type: 'UNUSED'
                }, {
                    name: '未开始',
                    num: 0,
                    type: 'NOT_STARTED'
                }, {
                    name: '已使用',
                    num: 0,
                    type: 'USED'
                }, {
                    name: '已过期',
                    num: 0,
                    type: 'EXPIRED'
                },
            ]
        },
        list: [],
        pageNum: 1,
        color: 'cardBox-orange',
        text: '去使用'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        layoutComponent = this.selectComponent('#layout')
        layoutComponent.refres(true)
    },
    /** 数据初始化 */
    dataInit() {
        this.setData({ pageNum: 1, list: '' })
        this.getData(true)
    },
    // 初次进入页面 + 分页加载数据
    async getData(init = false) {
        this.setData({
            'pageStatus.loading': true,
            'pageStatus.loadingMore': init == false ? 'loading' : 'hide'
        })
        let list = this.data.list
        let type = this.data.type
        // let result = await ToolServer.queryUserCoupon({
        //     status: this.data.type.list[this.data.type.active].type,
        //     pageNum: this.data.pageNum
        // })
        if (result.totalList.length > 0) {
            result.totalList.map(a => {
                type.list.map(b => {
                    if (a._id == b.type) {
                        b.num = a.total
                    }
                })
            })
        }

        if (list) {
            list.rows.push(...result.list.rows)
            this.setData({ list })
        } else {
            this.setData({ list: result.list })
        }

        layoutComponent.refres(false)
        this.setData({
            'pageStatus.loading': false,
            'pageStatus.layoutStatus': result.list.rows.length == 0 ? 'no-data' : 'normal',
            'pageStatus.loadingMore': this.data.pageNum * result.list.pageSize >= result.list.total ? 'no-data' : 'hide',
            type
        })
    },
    //选择类型
    async onChange(e) {
        let index = e.detail.index
        let color, text
        switch (index) {
            case 0:
                color = 'cardBox-orange'
                text = '去使用'
                break;
            case 1:
                color = 'cardBox-white'
                text = '未开始'
                break;
            case 2:
                color = 'cardBox-grey'
                text = '已使用'
                break;
            case 3:
                color = 'cardBox-gray'
                text = '已过期'
                break;
        }
        this.setData({
            'type.active': index,
            color, text
        })
        this.dataInit()
    },
    //下拉翻页
    async bindscrolltolower() {
        let list = this.data.list
        let pageNum = this.data.pageNum
        if (list.total > list.pageSize * pageNum) {
            pageNum++
            this.setData({ pageNum })
            await this.getData()
        }
    },
    //去使用
    bindNav() {
        wx.switchTab({
            url: '../../tabs/home/index',
        });
    },
})